/**   
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-11-24 22:18:21  
 * Copyright (c) 2015- joysuch-版权所有     
 */

package com.joysuch.wwyt.risk.service.impl;

import javax.transaction.Transactional;

import com.joysuch.wwyt.core.service.BizCodeGeneratorFacade;
import com.joysuch.wwyt.risk.entity.RiskEvaluationMethodResultItem;
import com.joysuch.wwyt.risk.entity.dsl.QRiskEvaluationMethodResultItem;
import com.joysuch.wwyt.risk.repository.RiskEvaluationMethodResultItemDao;
import com.joysuch.wwyt.risk.service.RiskEvaluationMethodResultItemService;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
@Transactional
public class RiskEvaluationMethodResultItemServiceImpl implements RiskEvaluationMethodResultItemService {

    @Autowired
    private RiskEvaluationMethodResultItemDao dao;
    @Autowired
    private BizCodeGeneratorFacade bizCodeGeneratorFacade;
    @Autowired
    private JPAQueryFactory jpaQueryFactory;

    @Override
    public void add(RiskEvaluationMethodResultItem entity) throws Exception {
        dao.save(entity);
    }

    @Override
    public void update(RiskEvaluationMethodResultItem entity) throws Exception {
        RiskEvaluationMethodResultItem content = dao.findById(entity.getId()).get();
        if (content != null) {
            // TODO set nessessary properties
            dao.save(entity);
        }
    }

    @Override
    public Page<RiskEvaluationMethodResultItem> findByPage(Pageable page) {
        return dao.findAll(page);
    }

    @Override
    public void delete(Long id) {
        dao.deleteById(id);
    }

    @Override
    public RiskEvaluationMethodResultItem findById(Long id) {
        return dao.findById(id).get();
    }

    @Override
    public RiskEvaluationMethodResultItem findByMethodAndLevelKey(String method, String key) {
        return dao.findByMethodAndLevelKey(method, key);
    }

    @Override
    public RiskEvaluationMethodResultItem findByMethodAndValue(String method, Float value) {
        QRiskEvaluationMethodResultItem item = QRiskEvaluationMethodResultItem.riskEvaluationMethodResultItem;
        Predicate p = ExpressionUtils.and(item.minValue.loe(value), item.maxValue.goe(value));
        p = ExpressionUtils.and(p, item.method.eq(method));
        return jpaQueryFactory.select(item).from(item).where(p).fetchOne();
    }

}
